package com.example.wan1.holder;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Outline;
import android.text.Html;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.holder.Holder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.example.wan1.R;
import com.example.wan1.app.MyApplication;
import com.example.wan1.model.BannerBean;
import com.example.wan1.utils.GlideRoundedCornersTransform;
import com.example.wan1.widget.ZQImageViewRoundOval;

import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
import retrofit2.http.OPTIONS;

/***
 * @Author: zxd
 * @CreateDate: 2020/6/30 14:02
 * @Description: 轮播图Holder
 * 关于轮播图出现这个问题“ Glide加载网络图片的时候先显示完整图片，再显示圆角图片”
 * 尝试1：setOutlineProvider
 * 尝试2：GlideRoundedCornersTransform
 * 尝试3：换控件，加载bitmap
 */
public class BannerImageHolderView extends Holder<BannerBean> {

    private Context mContext;
    private ImageView mImageView;
    private TextView mTextView;

    public BannerImageHolderView(View itemView, Context context) {
        super(itemView);
        this.mContext = context;
    }

    @Override
    protected void initView(View itemView) {
        mImageView = itemView.findViewById(R.id.home_banner_item_iv);
//        mImageView.setRoundRadius(50);
//        mImageView.setType(1);
        /*mImageView.setOutlineProvider(new ViewOutlineProvider() {

            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 15);
            }
        });
        mImageView.setClipToOutline(true);*/
        mTextView = itemView.findViewById(R.id.home_banner_item_tv);
    }

    @SuppressLint("CheckResult")
    @Override
    public void updateUI(BannerBean data) {
        /*RequestOptions options = new RequestOptions();
        options.
                centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)//设置缓存策略
                .transform(new GlideRoundedCornersTransform(50, GlideRoundedCornersTransform.CornerType.ALL));
        Glide.with(mContext).load(data.getImagePath()).apply(options).into(mImageView);*/

        mTextView.setText(Html.fromHtml(data.getTitle()));
        Glide.with(mContext).load(data.getImagePath()).apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)).into(mImageView);
        /*Glide.with(mContext).asBitmap().load(data.getImagePath()).apply(new RequestOptions().centerCrop()).
                into(new BitmapImageViewTarget(mImageView) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(MyApplication.getInstance().getResources(), resource);
                        roundedBitmapDrawable.setCornerRadius(50);
                        mImageView.setImageDrawable(roundedBitmapDrawable);
                    }
                });*/

    }
}
